// import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
// import 'package:flutter/widgets.dart';
import 'package:flutter_community/pages/house/components/house_item.dart';
import 'package:flutter_community/utils/eventBus.dart';
import 'package:flutter_community/utils/http.dart';

class HouseList extends StatefulWidget {
  const HouseList({super.key});

  @override
  State<HouseList> createState() => _HouseListState();
}

class _HouseListState extends State<HouseList> {
  //定义我的房屋列表数据
  List _houseListData = [];
//定于数据加载状态
  bool _isLoading = true;
  @override
  void initState() {
    super.initState();
    getHoustList();
    //刷新房屋列表
    eventBus.on<RefreshMyHouseEvent>().listen((event) {
      getHoustList();
    });
    //添加房屋时列表刷新
    eventBus.on<AddHouseRefreshEvent>().listen((event) {
      getHoustList();
    });
  }

  //获取房屋列表数据
  getHoustList() async {
    setState(() {
      _isLoading = true;
    });
    final res = await http.get('/room');
    print(res);
    setState(() {
      _houseListData = res['data'];
      _isLoading = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('我的房屋'),
        backgroundColor: const Color.fromRGBO(240, 238, 239, 1),
        centerTitle: true,
      ),
      body: Stack(
        children: [
          //顶部内容
          _isLoading
              ? const Center(
                  child: CircularProgressIndicator(
                    color: Color.fromARGB(255, 239, 170, 192),
                  ),
                )
              : Padding(
                  padding:
                      const EdgeInsets.only(bottom: 80, left: 10, right: 10),
                  child: ListView.builder(
                      itemCount: _houseListData.length,
                      itemBuilder: (BuildContext context, int index) {
                        return HouseItem(
                          houseInfo: _houseListData[index],
                        );
                      })),
          //底部
          Positioned(
              bottom: 10,
              left: 0,
              right: 0,
              child: Container(
                padding: const EdgeInsets.all(10),
                color: const Color.fromARGB(255, 248, 183, 183),
                //按钮
                child: Row(
                  children: [
                    Expanded(
                        child: ElevatedButton(
                      onPressed: () {
                        //跳转至添加房屋页
                        Navigator.pushNamed(context, '/location_list');
                      },
                      child: const Column(
                        children: [
                          SizedBox(height: 5),
                          Icon(Icons.add),
                          Text('添加房屋'),
                          SizedBox(height: 5)
                        ],
                      ),
                    ))
                  ],
                ),
              ))
        ],
      ),
    );
  }
}
